package org.game.porker.porker24.controller;

import java.util.ArrayList;
import java.util.List;

//import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.game.porker.porker24.mybatis.pojo.Player;
import org.game.porker.porker24.mybatis.service.DerbyDbSetupService;
import org.game.porker.porker24.mybatis.service.PlayerService;
import org.game.porker.porker24.util.JdbcPropertyReader;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessResourceFailureException;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/home")
public class HomeController {
	private static Logger logger = Logger.getLogger(HomeController.class);
	@Autowired
	private PlayerService playerService;
	@Autowired
	private DerbyDbSetupService derbyDbSetupService;

	public HomeController() {
		// TODO Auto-generated constructor stub
	}
	
	@RequestMapping("/index")
	public String index(ModelMap modelMap) {
		List<Player> top10 = new ArrayList<Player>();
		try {
			top10 = playerService.getTop10Players();
		} catch (Exception e) {
			logger.debug(e.getClass().getName());
			logger.debug(e.getCause().getClass().getName());
//			throw new RuntimeException(e);
			if (e.getClass().getName().equals(DataAccessResourceFailureException.class.getName())) {
				//数据库可能还没创建
				derbyDbSetupService.setupDeryDb(JdbcPropertyReader.getDbUsername(), 
						JdbcPropertyReader.getDbPassword());
				
			} else {
				throw new RuntimeException(e);
			}
		}
		modelMap.addAttribute("top10", top10);
		return "index";
	}

}
